//
//  ViewController.m
//  简单的计算器
//
//  Created by apple on 15/8/30.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

{
    NSNumber *_result;//结果
    
    NSNumber *_firstNumber;
    NSNumber *_secondNumber;
    
    NSString *_operator;//运算符
    
    NSMutableString *_str;
    
    BOOL allowPoint;//一个数字里只准许存在一个点；
}
@property (weak, nonatomic) IBOutlet UILabel *displayLabel;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _str = [NSMutableString string];
    [_str appendFormat:@"0"];
    _displayLabel.text = _str;
    
    allowPoint = YES;
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

//数字按键

- (IBAction)numberButtonClick:(UIButton *)sender
{
    [self defaultConfig];
    [_str appendString:sender.currentTitle];
    _displayLabel.text = _str;

}

//点
- (IBAction)dotButtonClick:(UIButton *)sender
{
    if (allowPoint)
    {
        [_str appendFormat:@"."];
        allowPoint = NO;
    }
    _displayLabel.text = _str;
}

//清除
- (IBAction)removeButtonClick:(UIButton *)sender
{
    [_str deleteCharactersInRange:NSMakeRange(0, _str.length)];
   
    NSLog(@"%@",_str);
     _displayLabel.text = @"0";
    allowPoint = YES;
}

- (void)defaultConfig
{
   if(_str.integerValue == 0)
   {
       if (![_str containsString:@"."])
       {
           [_str deleteCharactersInRange:NSMakeRange(0, _str.length)];
       }
   }

}

//撤销
- (IBAction)undoButtonClick:(UIButton *)sender
{
    NSUInteger a = [_str length] - 1;
    
    [_str deleteCharactersInRange:NSMakeRange(a, 1)];
    
    _displayLabel.text = _str;
}

//加法
- (IBAction)addButtonClick:(UIButton *)sender
{
    [self algorithmWithOperator:@"add"];
}

//减法
- (IBAction)subtractionButtonClick:(UIButton *)sender
{
    [self algorithmWithOperator:@"sub"];
}

//乘法
- (IBAction)mulButtonClick:(UIButton *)sender
{
    
    [self algorithmWithOperator:@"mul"];

}

//除法
- (IBAction)divButtonClick:(UIButton *)sender
{
    [self algorithmWithOperator:@"div"];
}

//等于
- (IBAction)equalButtonClick:(UIButton *)sender
{
    _secondNumber = @(_str.floatValue);
    
    if ([_operator isEqualToString:@"add"])
    {

    _result = [NSNumber numberWithFloat:(
        [_firstNumber floatValue] + [_secondNumber floatValue])];
        
        NSString *string = [NSString stringWithFormat:@"%@",_result];
        
        _displayLabel.text = string;
        
        allowPoint = YES;
        
        [self reset];
    }

    else if ([_operator isEqualToString:@"sub"])
    {
        _secondNumber = @(_str.floatValue);
        
        _result = [NSNumber numberWithFloat:([_firstNumber floatValue] - [_secondNumber floatValue])];
        
        NSString *string = [NSString stringWithFormat:@"%@",_result];
        
        _displayLabel.text = string;
        
        allowPoint = YES;
        
        [self reset];
    }
    
    else if ([_operator isEqualToString:@"mul"])
    {
        _secondNumber = @(_str.floatValue);
        
        _result = [NSNumber numberWithFloat:([_firstNumber floatValue] * [_secondNumber floatValue])];
        
        NSString *string = [NSString stringWithFormat:@"%@",_result];
        
        _displayLabel.text = string;
        
        allowPoint = YES;
        
        [self reset];
 
    }
    else if ([_operator isEqualToString:@"div"])
    {
        _secondNumber = @(_str.floatValue);
        
        _result = [NSNumber numberWithFloat:([_firstNumber floatValue] / [_secondNumber floatValue])];
        
        NSString *string = [NSString stringWithFormat:@"%@",_result];
        
        _displayLabel.text = string;
        
        allowPoint = YES;
        
        [self reset];
   
    }
}

- (void)algorithmWithOperator:(NSString *)operator
{
    _firstNumber = @(_str.floatValue);
    
    [_str deleteCharactersInRange:NSMakeRange(0, _str.length)];
    
    _operator = operator;
    
    allowPoint = YES;
}

//连续运算；
- (void)reset
{
    [_str deleteCharactersInRange:NSMakeRange(0, _str.length)];
    _secondNumber = _result;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
